/*
 * Created on Jun 6, 2004
 */
package dk.aiszone.lang.autosource.generatorobjects;

import dk.aiszone.lang.autosource.generatorobjects.helpers.ImportsList;
import dk.aiszone.lang.autosource.generatorobjects.interfaces.GeneratorObject;
import dk.aiszone.lang.autosource.generatorobjects.interfaces.Parent;

/**
 * Implementation of the most basic Java<sup>TM</sup> file.<br>
 * The class is mearly a wrapper used for ordering the contents of a java file in the proper order.<br>
 * <ol>
 * <li>Package info
 * <li>Imports
 * <li>The class itself
 * </ol>
 * 
 * @author <a href="mailto:ais@mail.dk">Ais</a>
 * @since 1.0
 * @version 1.0
 */
public class JavaFile extends GeneratorObject implements Parent {

    private AbstractBody<?> body;
    private ImportsList imports;
    private String packageName;

    /**
     * Default constructor for the <code>JavaFile</code> class.
     */
    public JavaFile() {
        imports = new ImportsList();
    }

    /**
     * Constructor for the <code>JavaFile</code> class.
     * 
     * @param packageName
     *            The package name of the <code>JavaFile</code>
     * @param body
     *            The file body
     */
    public JavaFile(String packageName, AbstractBody<?> body) {
        this();
        this.packageName = packageName;
        setBody(body);
    }

    /**
     * Add an <code>Import</code> to the <code>JavaFile</code>.<br>
     * This method is used for adding additional <code>Imports</code> to the <code>JavaFile</code>.<br>
     * <code>Imports</code> added through this method will be merged with the <code>Imports</code> extracted from the body implementation.<br>
     * Already added <code>Imports</code> will be ignored.
     * 
     * @param _import
     *            <code>Import</code> to add.
     */
    public void addImport(Import _import) {
        imports.addImport(_import);
    }

    /**
     * Retrieve the name of <code>JavaFile</code>.<br>
     * The name is extracted from <code>body</code> and appended &quot;.java&quot;
     * 
     * @return The name of the file being created.
     */
    public String getFileName() {
        return body.getName() + ".java";
    }

    /**
     * Retrieve the file path of the <code>JavaFile</code>.<br>
     * The result is the relative path of the file extracted from the package name.
     * 
     * @return The relative path of the <code>JavaFile</code>
     */
    public String getFilePath() {
        return packageName.replace('.', '/');
    }

    /**
     * Retrieve the <code>Imports</code> needed by the <code>JavaFile</code>
     * 
     * @return <code>ImportsList</code> containing the needed <code>Imports</code>.
     */
    public ImportsList getImports() {
        imports.addAll(body.getNeededImports());
        return imports;
    }

    /**
     * Retrieve the body of the <code>JavaFile</code>.<br>
     * 
     * @return The body of the file.
     */
    public AbstractBody<?> getMainClass() {
        return body;
    }

    /**
     * Retrieve the package of the <code>JavaFile</code>.
     * 
     * @return The file package
     */
    public String getPackage() {
        return packageName;
    }

    /**
     * Set or update the body of the <code>JavaFile</code>.
     * 
     * @param body
     *            The new body.
     */
    public void setBody(AbstractBody<?> body) {
        this.body = body;
        this.body.setParent(this);
    }

    /**
     * Set or update the package of the <code>JavaFile</code>.
     * 
     * @param packageName
     *            The new package.
     */
    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }
}